/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.adv.service;

import com.alibaba.fastjson.JSONObject;
import com.thinkgem.jeesite.common.aliyuncs.AliyuncsUtil;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.service.ServiceException;
import com.thinkgem.jeesite.common.utils.ImgUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hk.adv.dao.HkMediaAdvDao;
import com.thinkgem.jeesite.modules.hk.adv.entity.HkAllAdv;
import com.thinkgem.jeesite.modules.hk.adv.entity.HkMediaAdv;
import com.thinkgem.jeesite.modules.hk.data.service.HkAdvDataService;
import com.thinkgem.jeesite.modules.hk.sys.entity.adv.HkCategory;
import com.thinkgem.jeesite.modules.hk.sys.service.adv.HkCategoryService;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.util.UriUtils;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

/**
 * 自媒体广告Service
 *
 * @author 罗天文
 * @version 2018-03-15
 */
@Service
@Transactional(readOnly = true)
public class HkMediaAdvService extends CrudService<HkMediaAdvDao, HkMediaAdv> {
    @Autowired
    HkAllAdvService hkAllAdvService;
    @Autowired
    HkCategoryService hkCategoryService;

    @Override
    public HkMediaAdv get(String id) {
        return super.get(id);
    }

    @Override
    public List<HkMediaAdv> findList(HkMediaAdv hkMediaAdv) {
        return super.findList(hkMediaAdv);
    }

    @Override
    public Page<HkMediaAdv> findPage(Page<HkMediaAdv> page, HkMediaAdv hkMediaAdv) {
        return super.findPage(page, hkMediaAdv);
    }

    @Transactional(readOnly = false)
    public void save(HkMediaAdv hkMediaAdv) {
        boolean saveData=false;
        if(StringUtils.isEmpty(hkMediaAdv.getId())){
            saveData=true;
        }
        if (hkMediaAdv.getNote() != null) {
            hkMediaAdv.setNote(StringEscapeUtils.unescapeHtml4(hkMediaAdv.getNote()));
        }




        hkMediaAdv.setTag(null);
        if (null != hkMediaAdv.getTags()) {
            hkMediaAdv.setTag(String.join(",", hkMediaAdv.getTags()));
        }
       String coverImgSrc=hkMediaAdv.getCoverImgSrc();
        if (!coverImgSrc.contains(Global.getFileServer())) {
            coverImgSrc=(Global.getFileServer().trim() + hkMediaAdv.getCoverImgSrc());
            Map<String, String> map = ImgUtils.getImgWidthHeight(hkMediaAdv.getCoverImgSrc());
            hkMediaAdv.setCoverImgWidth(map.get("width"));
            hkMediaAdv.setCoverImgHeight(map.get("height"));
        }
        if(StringUtils.isNotEmpty(hkMediaAdv.getAimgSrc())) {

            String medisPath = Global.getUserfilesBaseDir()+ hkMediaAdv.getAimgSrc();
            try {
                medisPath = UriUtils.decode(medisPath, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                throw new ServiceException("上传失败");
            }
            StringBuffer sb=new StringBuffer();
            HkCategory category;
            for (String c:hkMediaAdv.getCategory().split(",")
            ) {
                category=hkCategoryService.get(c);
                sb.append(category.getName()).append(",");
            }
            String videoId = AliyuncsUtil.uploadVideo(hkMediaAdv.getTitle(), medisPath, coverImgSrc, sb.toString() );
            if(StringUtils.isEmpty(videoId)){
                throw new ServiceException("上传失败");
            }
            hkMediaAdv.setImgSrc(videoId);
        }
       /*
        * 调用上传接口  采用OSS方式上传
       String response = AliyuncsUtil.requestServer(hkMediaAdv,false);
        try {
            if (response != null && !"".equals(response)) {
                JSONObject jsonObject = JSONObject.parseObject(response);
                videoId = jsonObject.get("VideoId").toString();

            } else {
                return;
            }
        } catch (Exception e) {
            return;
        }*/


        super.save(hkMediaAdv);

        deleteTag(hkMediaAdv);
        insertTag(hkMediaAdv);
        deleteCategory(hkMediaAdv);
        insertCategory(hkMediaAdv);
        if (hkMediaAdv.getState().equals("1")) {
            insertAllAdv(hkMediaAdv);
        }

    }

    @Override
    @Transactional(readOnly = false)
    public void delete(HkMediaAdv hkMediaAdv) {
        super.delete(hkMediaAdv);
    }


    /**
     * 删除标签
     *
     * @param hkMediaAdv
     * @return
     */
    @Transactional(readOnly = false)
    public void deleteCategory(HkMediaAdv hkMediaAdv) {
        super.dao.deleteCategory(hkMediaAdv);
    }

    /**
     * 增加标签
     *
     * @param hkMediaAdv
     * @return
     */
    @Transactional(readOnly = false)
    public void insertCategory(HkMediaAdv hkMediaAdv) {
        super.dao.insertCategory(hkMediaAdv);
    }

    //同步数据
    @Transactional(readOnly = false)
    public void insertAllAdv(HkMediaAdv hkMediaAdv) {


        String sourceId = hkMediaAdv.getId();
        HkAllAdv hkAllAdvOld = hkAllAdvService.getBySourceId(sourceId);
        if (null == hkAllAdvOld) {
            HkAllAdv hkAllAdv = new HkAllAdv();
            BeanUtils.copyProperties(hkMediaAdv, hkAllAdv);
            hkAllAdv.setId(null);
            hkAllAdv.setSourceId(hkMediaAdv.getId());
            //类型  1自媒体 2企业广告3城市广告4传统文化
            hkAllAdv.setType("1");
            hkAllAdvService.save(hkAllAdv);
        } else {
            HkAllAdv hkAllAdv = hkAllAdvOld;
            BeanUtils.copyProperties(hkMediaAdv, hkAllAdv);
            hkAllAdv.setId(hkAllAdvOld.getId());
            hkAllAdvService.save(hkAllAdv);
        }

        hkAllAdvService.deleteTag(hkMediaAdv.getId(), hkMediaAdv.getTag());
        hkAllAdvService.insertTag(hkMediaAdv.getId(), hkMediaAdv.getTag());
        hkAllAdvService.deleteCategory(hkMediaAdv.getId(), hkMediaAdv.getCategory());
        hkAllAdvService.insertCategory(hkMediaAdv.getId(), hkMediaAdv.getCategory());
    }

    /**
     * 删除标签
     *
     * @param hkMediaAdv
     * @return
     */
    @Transactional(readOnly = false)
    public void deleteTag(HkMediaAdv hkMediaAdv) {
        super.dao.deleteTag(hkMediaAdv);
    }

    /**
     * 增加标签
     *
     * @param hkMediaAdv
     * @return
     */
    @Transactional(readOnly = false)
    public void insertTag(HkMediaAdv hkMediaAdv) {
        //super.dao.insertTag(hkMediaAdv);
    }

    @Transactional(readOnly = false)
    public void pass(HkMediaAdv hkMediaAdv) {
        String ids[] = hkMediaAdv.getId().split(",");
        for (String id : ids
        ) {
            super.dao.pass(new HkMediaAdv(id));
        }

    }

    @Transactional(readOnly = false)
    public void nopass(HkMediaAdv hkMediaAdv) {
        String ids[] = hkMediaAdv.getId().split(",");
        for (String id : ids
        ) {
            super.dao.nopass(new HkMediaAdv(id));
        }

    }
}